/**
 * <p>
 * LocalIpUtils
 * </p>
 *
 * @author: lianjun
 * @since: 2022-02-16
 */
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

@Slf4j
public class LocalIpUtils {
    private LocalIpUtils() {
    }

    /**
     * 下面这种方式在Linux不能获取本机IP：
     *
     * <pre>
     * InetAddress inet = InetAddress.getLocalHost();
     * </pre>
     * <p>
     * 本方法改用网卡的绑定地址，可以取到本机的ip地址
     *
     * @return
     * @throws SocketException
     */
    public static InetAddress getLocalAddress() throws IOException {
        InetAddress ip = null;
        Enumeration<?> e1 = NetworkInterface
                .getNetworkInterfaces();
        out:
        while (e1.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) e1.nextElement();
            Enumeration<?> e2 = ni.getInetAddresses();
            while (e2.hasMoreElements()) {
                InetAddress ia = (InetAddress) e2.nextElement();
                if (ia instanceof Inet6Address) {
                    continue;
                }
                if (ipCheck(ia)) {
                    ip = ia;
                    break out;
                }
            }
        }
        return ip;
    }

    public static String getLocalHostSuffix() {
        try {
            InetAddress localHost = getLocalAddress();
            String ipAddress = localHost.getLocalHost().toString();
            log.info("localIpAddress:" + ipAddress);
            return ipAddress.substring(ipAddress.lastIndexOf('.') + 1);
        } catch (Exception e) {
            log.error("未能成功获取ip", e);
        }
        return null;
    }

    private static boolean ipCheck(InetAddress ia) {
        return !ia.isMCNodeLocal() && !ia.isMCSiteLocal() && !ia.isAnyLocalAddress() && !ia.isMCGlobal() && !ia.isLoopbackAddress() &&
                ia.isSiteLocalAddress() && !ia.isLinkLocalAddress() && !ia.isMCLinkLocal() && !ia.isMCOrgLocal() && !ia.isMulticastAddress();
    }
}
